﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Xml;
using Jurassic;
using Flower.Directory.Scripting;

namespace Flower.Directory.Management.Tests
{
    [TestFixture]
    public class JsonToSpringContainerTests
    {
        private const string XMLNS = "http://www.springframework.net";
        private static readonly ScriptEngine Engine = 
            Scripting.Setup.InitEngine(Directory.Scripting.Setup.CreateEngine());

        private XmlNode EvalTest(string script)
        {
            return ((XmlObject)Engine.Evaluate(script)).Xml;
        }

        [Test]
        public void ObjectWithProperties()
        {
            XmlNode xml = 
                EvalTest
                (
                    "__jsonToSpringContainer({ " +
                    "obj1: { $type: 'type1', a: 'a_val', b: 1, c: true, d: null }," +
                    "obj2: { $type: 'type2', e: 'e_val', f: 2, g: false, h: { $ref: 'obj1' } }," +
                    "})"
                );
            XmlNamespaceManager xmlNs = new XmlNamespaceManager(((XmlDocument)xml).NameTable);
            xmlNs.AddNamespace("s", XMLNS);

            var obj1 = xml.SelectSingleNode("/s:objects/s:object[@id = 'obj1']", xmlNs);
            XmlNode node;

            Assert.IsNotNull(obj1);
            Assert.IsNotNull(obj1.Attributes["type"]);
            Assert.AreEqual("type1", obj1.Attributes["type"].Value);
            Assert.IsNotNull(obj1.SelectSingleNode("s:property[@name = 'a']", xmlNs));
            Assert.IsTrue
            (
                (node = obj1.SelectSingleNode("s:property[@name = 'a']/s:value", xmlNs)) != null &&
                node.InnerText == "a_val"
            );
            Assert.IsTrue
            (
                (node = obj1.SelectSingleNode("s:property[@name = 'b']/s:value", xmlNs)) != null &&
                node.InnerText == "1"
            );
            Assert.IsTrue
            (
                (node = obj1.SelectSingleNode("s:property[@name = 'c']/s:value", xmlNs)) != null &&
                node.InnerText == "true"
            );
            Assert.IsNotNull(obj1.SelectSingleNode("s:property[@name = 'd']/s:null", xmlNs));

            var obj2 = xml.SelectSingleNode("/s:objects/s:object[@id = 'obj2']", xmlNs);

            Assert.IsNotNull(obj2);
            Assert.IsNotNull(obj2.Attributes["type"]);
            Assert.AreEqual("type2", obj2.Attributes["type"].Value);
            Assert.IsNotNull(obj2.SelectSingleNode("s:property[@name = 'e']", xmlNs));
            Assert.IsTrue
            (
                (node = obj2.SelectSingleNode("s:property[@name = 'e']/s:value", xmlNs)) != null &&
                node.InnerText == "e_val"
            );
            Assert.IsTrue
            (
                (node = obj2.SelectSingleNode("s:property[@name = 'f']/s:value", xmlNs)) != null &&
                node.InnerText == "2"
            );
            Assert.IsTrue
            (
                (node = obj2.SelectSingleNode("s:property[@name = 'g']/s:value", xmlNs)) != null &&
                node.InnerText == "false"
            );
            Assert.IsTrue
            (
                (node = obj2.SelectSingleNode("s:property[@name = 'h']/s:ref", xmlNs)) != null &&
                node.Attributes["object"].Value == "obj1"
            );
        }

        [Test]
        public void ObjectWithConstructor()
        {
            XmlNode xml =
                EvalTest
                (
                    "__jsonToSpringContainer({ " +
                    "obj1: { $type: 'T', $constructor: [ 's', 1, true, null, { $type: 'T' }, { $ref: 'obj1' } ] }," +
                    "obj2: { $type: 'T', $constructor: { a: 's', b: 1, c: true, d: null, e: { $type: 'T' }, f: { $ref: 'obj2' } } }," +
                    "})"
                );
            XmlNamespaceManager xmlNs = new XmlNamespaceManager(((XmlDocument)xml).NameTable);
            xmlNs.AddNamespace("s", XMLNS);

            var obj1 = xml.SelectSingleNode("/s:objects/s:object[@id = 'obj1']", xmlNs);
            XmlNode node;

            var constArgs = obj1.SelectNodes("s:constructor-arg", xmlNs);

            Assert.AreEqual(6, constArgs.Count);
            Assert.AreEqual("s", constArgs[0].SelectSingleNode("s:value", xmlNs).InnerText);
            Assert.AreEqual("1", constArgs[1].SelectSingleNode("s:value", xmlNs).InnerText);
            Assert.AreEqual("true", constArgs[2].SelectSingleNode("s:value", xmlNs).InnerText);
            Assert.IsNotNull(constArgs[3].SelectSingleNode("s:null", xmlNs));
            Assert.IsNotNull(constArgs[4].SelectSingleNode("s:object", xmlNs));
            Assert.IsNotNull(constArgs[5].SelectSingleNode("s:ref", xmlNs));

            var obj2 = xml.SelectSingleNode("/s:objects/s:object[@id = 'obj2']", xmlNs);

            Assert.IsTrue
            (
                (node = obj2.SelectSingleNode("s:constructor-arg[@name = 'a']/s:value", xmlNs)) != null &&
                node.InnerText == "s"
            );
            Assert.IsTrue
            (
                (node = obj2.SelectSingleNode("s:constructor-arg[@name = 'b']/s:value", xmlNs)) != null &&
                node.InnerText == "1"
            );
            Assert.IsTrue
            (
                (node = obj2.SelectSingleNode("s:constructor-arg[@name = 'c']/s:value", xmlNs)) != null &&
                node.InnerText == "true"
            );
            Assert.IsNotNull(obj2.SelectSingleNode("s:constructor-arg[@name = 'd']/s:null", xmlNs));
            Assert.IsNotNull(obj2.SelectSingleNode("s:constructor-arg[@name = 'e']/s:object", xmlNs));
            Assert.IsNotNull(obj2.SelectSingleNode("s:constructor-arg[@name = 'f']/s:ref", xmlNs));
        }

        [Test]
        public void DictionariesAndLists()
        {
            XmlNode xml =
                EvalTest
                (
                    "__jsonToSpringContainer({ " +
                    "obj1: { $type: 'T', l: [1, 's', null, { $type: 'T' }] }," +
                    "obj2: { $type: 'T', d: { a: 1, b: 's', c: null, d: { $type: 'T' } } }," +
                    "})"
                );
            XmlNamespaceManager xmlNs = new XmlNamespaceManager(((XmlDocument)xml).NameTable);
            xmlNs.AddNamespace("s", XMLNS);

            var obj1 = xml.SelectSingleNode("/s:objects/s:object[@id = 'obj1']", xmlNs);
            XmlNode node;

            Assert.IsNotNull(node = obj1.SelectSingleNode("s:property[@name = 'l']/s:list", xmlNs));
            Assert.AreEqual(4, node.ChildNodes.Count);
            Assert.IsTrue(node.ChildNodes[0].LocalName == "value" && node.ChildNodes[0].InnerText == "1");
            Assert.IsTrue(node.ChildNodes[1].LocalName == "value" && node.ChildNodes[1].InnerText == "s");
            Assert.IsTrue(node.ChildNodes[2].LocalName == "null");
            Assert.IsTrue(node.ChildNodes[3].LocalName == "object");

            var obj2 = xml.SelectSingleNode("/s:objects/s:object[@id = 'obj2']", xmlNs);
            XmlNode dict;

            Assert.IsNotNull(dict = obj2.SelectSingleNode("s:property[@name = 'd']/s:dictionary", xmlNs));
            Assert.IsTrue
            (
                (node = dict.SelectSingleNode("s:entry[@key = 'a']/s:value", xmlNs)) != null &&
                node.InnerText == "1"
            );
            Assert.IsTrue
            (
                (node = dict.SelectSingleNode("s:entry[@key = 'b']/s:value", xmlNs)) != null &&
                node.InnerText == "s"
            );
            Assert.IsNotNull(dict.SelectSingleNode("s:entry[@key = 'c']/s:null", xmlNs));
            Assert.IsNotNull(dict.SelectSingleNode("s:entry[@key = 'd']/s:object", xmlNs));
        }
    }
}
